package com.liu.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.user.entity.base.User;
import com.liu.user.entity.qo.UserRegisterInfoQO;
import com.liu.user.entity.vo.DeductionInfoVO;
import com.liu.user.entity.vo.UserInfoVO;
import com.liu.user.mapper.UserMapper;
import com.liu.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void register(UserRegisterInfoQO userRegisterInfoQO) {
        userMapper.registerUser(userRegisterInfoQO);
    }

    @Override
    public User registerCheckUsername(String username) {
        return userMapper.registerCheckUsername(username);
    }

    @Override
    public User login(String username) {
        return userMapper.registerCheckUsername(username);
    }

    @Override
    public void recordUserInfo(User user) {
        userMapper.recordUserInfo(user);
    }

    @Override
    public UserInfoVO getUserInfo(Integer userId) {
        return userMapper.getUserInfo(userId);
    }

    @Override
    public void modifyPassword(String newPassword, Integer id) {
        userMapper.modifyPassword(newPassword, id);
    }

    @Override
    public PageInfo<DeductionInfoVO> queryDeductionInfo(String userId,String pageNum,String pageSize) {
        PageInfo<DeductionInfoVO> deductionInfoVOPageInfo = PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize)).doSelectPageInfo(() -> {
            userMapper.queryDeductionInfo(userId);
        });
        // 新建分页
        PageInfo<DeductionInfoVO> deductionInfoVOPageInfoResult = new PageInfo<>();
        // 复制分页属性
        BeanUtils.copyProperties(deductionInfoVOPageInfo, deductionInfoVOPageInfoResult);
        // 对查询的list进行下一步操作
        List<DeductionInfoVO> deductionInfoVOS = new ArrayList<>(deductionInfoVOPageInfoResult.getList());
        deductionInfoVOPageInfoResult.setList(deductionInfoVOS);
        return deductionInfoVOPageInfoResult;
    }
}
